
var mirrarray = require("mirrarray");

function test() {
	var data = ['this', 'that', 'another', 'this']
	var keymirror = mirrarray(data);
	console.log(keymirror)
	console.log(mirrarray([null]))

	console.log('-----------------------\n')

	console.log(mirrArray(data))
	console.log(mirrArray([null]))
}


const isValidKey = element => {
	const isNull = element === null

	return ['number', 'string', 'boolean', 'undefined'].includes(typeof element) || isNull
}


let keysSeen = {}
const nonOverlappingKey = element => {

	const isNull = element === null
	const thisType = isNull ? 'null' : typeof element
	const typeSeenBefore = keysSeen['' + element]

	if(typeSeenBefore) {
		return typeSeenBefore === thisType
	} else {
		keysSeen['' + element] = thisType
		return true;	
	}

}




// var data = [{name: '刘阿勇', age:12}, new Date(), Math, /1/]
var data =  [1, '1']  // [true, 'true'];
console.log(mirrArrayV2(data))
console.log(mirrarray(data))



function mirrArrayV2(arr) {
	if(!Array.isArray(arr)) {
		throw new TypeError('Input to this must be an array!')
	}

	return arr.reduce((acc, key) => {
		if(!isValidKey(key)) {
			throw TypeError('Invalid element contained within input array; each element must be either a string or number!')
		}
		if(!nonOverlappingKey(key)) {
			throw TypeError('Distinct elements in the input array are coercing to the same string!')
		}
		return acc[key] = key
	}, {})
}



function mirrArray (arr) {
	var one, obj = {}
	for (var i = 0; i < arr.length; i++) {
		one = arr[i]
		obj[one] = one
	}
	return obj
}